/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.xuidemo.slice.edittext;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;

import com.xuexiang.xui_lib.component.edittext.materialedittext.MaterialEditText1;
import com.xuexiang.xui_lib.component.edittext.materialedittext.validation.RegexpValidator;
import com.xuexiang.xuidemo.ResourceTable;

/**
 * 多种自定义Edittext
 *
 * @since 2021-04-28
 */
public class MeterialEditAbilitySlice extends AbilitySlice {
    private Image backImage;
    private Button enableBt;
    private MaterialEditText1 materialEditText1;
    private Button setErrorBt;
    private Button setError2Bt;
    private Button setError3Bt;
    private MaterialEditText1 bottomTextEt;
    private MaterialEditText1 etPhoneNumber;
    private MaterialEditText1 etNotAllowEmpty1;
    private MaterialEditText1 etNotAllowEmpty2;
    private MaterialEditText1 validationEt;
    private Button validateBt;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_meterial_edittext);
        backImage = (Image) findComponentById(ResourceTable.Id_backImage);
        backImage.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminate();
            }
        });
        init();
        initView();
        initValidation();
    }

    /**
     * enable与disable切换
     */
    public void init() {
        enableBt = (Button) findComponentById(ResourceTable.Id_enableBt);
        materialEditText1 = (MaterialEditText1) findComponentById(ResourceTable.Id_basicEt);
        enableBt.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (enableBt.getText().toString().equals("DISABLE")) {
                    enableBt.setText("ENABLE");
                    materialEditText1.setEnabled(false);
                    materialEditText1.setTouchFocusable(false);
                    materialEditText1.clearFocus();
                    materialEditText1.setFocusable(Component.FOCUS_DISABLE);
                    materialEditText1.invalidate();
                } else {
                    enableBt.setText("DISABLE");
                    materialEditText1.setEnabled(true);
                    materialEditText1.setTouchFocusable(true);
                    materialEditText1.setFocusable(Component.FOCUS_ENABLE);
                    materialEditText1.invalidate();
                }
            }
        });
    }

    /**
     * 1,2,3中错误显示切换
     */
    public void initView() {
        setErrorBt = (Button)findComponentById(ResourceTable.Id_setErrorBt);
        setError2Bt = (Button)findComponentById(ResourceTable.Id_setError2Bt);
        setError3Bt = (Button)findComponentById(ResourceTable.Id_setError3Bt);
        bottomTextEt = (MaterialEditText1) findComponentById(ResourceTable.Id_bottomTextEt);
        setErrorBt.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                bottomTextEt.setHelperText("1-line Error!");
            }
        });
        setError2Bt.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                bottomTextEt.setHelperText("2-line\n" + "Error!");
            }
        });
        setError3Bt.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                bottomTextEt.setHelperText("So Many Errors!So Many Errors!So Many Errors!So "
                    + "Many Errors!So Many Errors!So Many Errors!So Many Errors!So Many Errors!");
            }
        });
    }

    /**
     * 正则表达式显示错误
     */
    public void initValidation() {
        etPhoneNumber = (MaterialEditText1) findComponentById(ResourceTable.Id_et_phone_number);
        etNotAllowEmpty1 = (MaterialEditText1) findComponentById(ResourceTable.Id_et_not_allow_empty1);
        etNotAllowEmpty2 = (MaterialEditText1) findComponentById(ResourceTable.Id_et_not_allow_empty2);
        validationEt = (MaterialEditText1) findComponentById(ResourceTable.Id_validationEt);
        validateBt = (Button)findComponentById(ResourceTable.Id_validateBt);
        etPhoneNumber.addValidator(new RegexpValidator("无效手机号!", "^\\d{11}$"));
        etNotAllowEmpty1.addValidator(new RegexpValidator("输入不能为空!", "^\\w+"));
        etNotAllowEmpty2.addValidator(new RegexpValidator("your name not allow empty!", "^\\w+"));
        validationEt.addValidator(new RegexpValidator("Only Integer Valid!", "\\d+"));
        validateBt.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                etPhoneNumber.validate();
                etNotAllowEmpty1.validate();
                etNotAllowEmpty2.validate();
                validationEt.validate();
            }
        });
    }
}

